package com.cqt.shopweb.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;

import com.cqt.shopdbutil.util.DButil;
import com.cqt.shoplogutil.utils.LogUtil;
import com.cqt.shopweb.entity.UserInfo;

public class UserDaoImpl {
	//添加用户
	public int addUser(UserInfo user){
		int result=0;
		Connection con=null;
		PreparedStatement stm=null;
		try{
			con=DButil.getCon();
			String sql="insert into userinfo(username,password,note) values(?,?,?)";
			stm=con.prepareStatement(sql);
			stm.setString(1, user.getUsername());
			stm.setString(2, user.getPassword());
			stm.setString(3, user.getNote());
			result=stm.executeUpdate();
			//添加成功后，写日志
			LogUtil.writeInfoMessage("添加用户成功!");
		}catch(Exception e){
			e.printStackTrace();
			LogUtil.writeErrorMessage("添加用户失败");
		}finally{
			DButil.close(null, stm, con);
		}
		return result;
	}
	//删除用户
	public int deleteUser(int id){
		int result=0;
		Connection con=null;
		PreparedStatement stm=null;
		try{
			con=DButil.getCon();
			String sql="delete from userinfo where id=?";
			stm=con.prepareStatement(sql);
			stm.setInt(1, id);
			result=stm.executeUpdate();
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DButil.close(null, stm, con);
		}
		return result;
	}
}
